package com.tepth.remote.core.app;

import android.content.Context;
import android.os.Handler;

import com.blankj.utilcode.util.Utils;
import com.joanzapata.iconify.IconFontDescriptor;
import com.joanzapata.iconify.Iconify;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Description:配置工具类
 *
 * @author Hequn.Lee
 * @date 2018/11/7
 */
public class Configurator {

    /**
     * 之所以不用WeakHashMap作为配置项的缓存，是因为WeakHashMap当系统不使用它的时候会回收里里面的键值对，所以用HashMap
     */
    private static final HashMap<Object, Object> CONTROL_CONFIGS = new HashMap<>();

    /**
     * 全局的Handler
     */
    private static final Handler HANDLER = new Handler();

    /**
     * 全局的图标库
     */
    private static final ArrayList<IconFontDescriptor> ICONS = new ArrayList<>();

    /**
     * 实例化全局配置
     */
    private Configurator() {
        CONTROL_CONFIGS.put(ConfigKeys.CONFIG_READY, false);
    }

    /**
     * 让外部通过调用getInstance()方法来返回唯一的实例。
     *
     * @return 返回唯一实例
     */
    public static Configurator getInstance() {
        return Holder.INSTANCE;
    }

    /**
     * 获取全局配置文件
     *
     * @return 配置文件
     */
    final HashMap<Object, Object> getControlConfigs() {
        return CONTROL_CONFIGS;
    }

    /**
     * 单例模式
     */
    private static class Holder {
        private static final Configurator INSTANCE = new Configurator();
    }

    /**
     * 初始化配置
     */
    public final void configure() {
        initIcons();
        Logger.addLogAdapter(new AndroidLogAdapter());
        CONTROL_CONFIGS.put(ConfigKeys.CONFIG_READY, true);
        CONTROL_CONFIGS.put(ConfigKeys.HANDLER, HANDLER);
        CONTROL_CONFIGS.put(ConfigKeys.ALARM_MANAGER,
                Control.getApplicationContext().getSystemService(Context.ALARM_SERVICE));
        Utils.init(Control.getApplicationContext());
    }

    /**
     * 初始化图标库
     */
    private void initIcons() {
        if (ICONS.size() > 0) {
            final Iconify.IconifyInitializer initializer = Iconify.with(ICONS.get(0));
            for (int i = 1; i < ICONS.size(); i++) {
                initializer.with(ICONS.get(i));
            }
        }
    }

    /**
     * 设置Soap请求主地址
     *
     * @param host 主地址
     * @return 返回配置类
     */
    public final Configurator withSoapHost(String host) {
        CONTROL_CONFIGS.put(ConfigKeys.SOAP_HOST, host);
        return this;
    }

    /**
     * 设置Retrofit请求主地址
     *
     * @param host 主地址
     * @return 返回配置类
     */
    public final Configurator withRetrofitHost(String host) {
        CONTROL_CONFIGS.put(ConfigKeys.RETROFIT_HOST, host);
        return this;
    }

    /**
     * 设置图标库
     *
     * @param descriptor 图标库
     * @return 返回配置类
     */
    public final Configurator withIcon(IconFontDescriptor descriptor) {
        ICONS.add(descriptor);
        return this;
    }

    /**
     * 检查配置是否初始化
     */
    private void checkConfiguration() {
        final boolean isReady = (boolean) CONTROL_CONFIGS.get(ConfigKeys.CONFIG_READY);
        if (!isReady) {
            throw new RuntimeException("Configuration is not ready,call configure");
        }
    }

    /**
     * 获取配置对象
     *
     * @param key 配置名称
     * @param <T> 配置对象
     * @return 返回配置对象
     */
    @SuppressWarnings("unchecked")
    final <T> T getConfiguration(Object key) {
        checkConfiguration();
        final Object value = CONTROL_CONFIGS.get(key);
        if (value == null) {
            throw new NullPointerException(key.toString() + " IS NULL");
        }
        return (T) CONTROL_CONFIGS.get(key);
    }
}
